<script setup>
import {inject, reactive, ref, watch, onMounted, onUnmounted} from "vue";
import {getRelation} from "@/api/AuthApi.js";
import CpsYy from "@/components/svg/Cps-yy.vue";
import CpsJp from "@/components/svg/Cps-jp.vue";
import UploadPic from "@/components/svg/UploadPic.vue";
import Navigation from "@/components/svg/Navigation.vue";
import CpsTj from "@/components/svg/Cps-tj.vue";
import More from "@/components/svg/More.vue";
import Menu from "@/components/svg/Menu.vue";
import Plus from "@/components/svg/Plus.vue";
import ClickBtn from "@/components/ClickBtn.vue";
import MpcUtil from "@/util/MpcUtil.js";
import UploadBtn from "@/components/UploadBtn.vue";
import MessageBox from "@/components/MessageBox.vue";
import Back from "@/components/svg/Back.vue";
import FullScreen from "../svg/FullScreen.vue";


let chunks = [];
const recordBtn = document.querySelector(".mediaBtn");
let mediaRecorder;

if (navigator.mediaDevices.getUserMedia) {
  const constraints = { audio: true };
  navigator.mediaDevices.getUserMedia(constraints).then(
    stream => {
      console.log("授权成功！");
      mediaRecorder = new MediaRecorder(stream);
      mediaRecorder.ondataavailable = function(e) {
        chunks.push(e.data);
        console.log(">>"+window.URL.createObjectURL(chunks[chunks.length - 1]));
        console.log(chunks[chunks.length - 1])
        console.log("录音器状态：", mediaRecorder.state);
        const msg = MpcUtil.sendAudio(1, currentChat.id, chunks[chunks.length - 1]);
        msg.my = true;
        msg.src = window.URL.createObjectURL(chunks[chunks.length - 1]);
        msgHis.value.push(msg);
      };
    },
    () => {
      console.error("授权失败！");
    }
  );
} else {
  console.error("浏览器不支持 getUserMedia");
}


defineProps({
  isShow: {
    type: Object,
    required: true,
    default: {}
  }
});

const myUser = inject("myUser");
const boxIndex = ref(1);
const chatList = ref([]);
const friendList = ref([]);
const currentChat = inject("currentChat");
const isText = ref(true);
const allMsgHis = {};
const msgHis = ref([]);
// 发送的文字
const chatText = ref("");
// 搜索文字
const searchText = ref("");
// 是否显示用户详情侧边栏
const isShowRight = ref(false);
// 是否显示下拉菜单
const showDropdown = ref(false);
// 是否显示创建群组页面
const showCreateGroup = ref(false);
// 群组名称
const groupName = ref('');
// 选中的好友列表
const selectedFriends = ref([]);
// 发送语音按钮文字
const sendAudioText = ref("点击开始录音");
// 添加全屏状态变量
const isFullscreen = ref(false);
// 是否显示聊天记录
const showHistory = ref(false);
// 搜索文字
const historySearchText = ref("");
// 消息类型筛选
const messageTypeFilter = ref("all"); // all, text, audio, image
// 时间范围筛选
const timeRangeFilter = ref("all"); // all, today, week, month
// 当前显示的消息类型标签
const activeMessageTab = ref("all");


/**
 * 图像转Base64
 */
function getBase64Image(img) {
  const canvas = document.createElement("canvas");
  canvas.width = img.width;
  canvas.height = img.height;
  var ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0, img.width, img.height);
  var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
  var dataURL = canvas.toDataURL("image/" + ext);
  return dataURL;
}
/**
 *Base64字符串转二进制
 */
function dataURLtoBlob(dataurl) {
  var arr = dataurl.split(','),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], {
    type: mime
  });
}
function readImg (file) {
  return new Promise((resolve, reject) => {
    const img = new Image()
    const reader = new FileReader()
    reader.onload = function (e) {
      img.src = e.target.result
    }
    reader.onerror = function (e) {
      reject(e)
    }
    reader.readAsDataURL(file)
    img.onload = function () {
      resolve(img)
    }
    img.onerror = function (e) {
      reject(e)
    }
  })
}
function fileToBlob(file) {
  return new Blob([file], { type: file.type });
}

function chatting(chat) {
  allMsgHis[currentChat.id] = msgHis.value;
  if (allMsgHis[chat.id]===undefined) {
    msgHis.value = [];
  } else {
    msgHis.value = allMsgHis[chat.id];
  }
  for (let index in chat) {
    currentChat[index] = chat[index];
  }
}

function needChat(friend) {
  chatList.value = chatList.value.filter(function(item) {
    return item.id !== friend.id;
  });
  chatList.value.unshift(friend);
  boxIndex.value = 1;
  chatting(friend);
}

function send() {
  if (isText.value&&chatText.value!=="") {
    const msg = MpcUtil.sendChat(1, currentChat.id, chatText.value);
    chatText.value = "";
    msg.my = true;
    msgHis.value.push(msg);
  }
}
function sendPic() {
  // 创建一个input元素
  const input = document.createElement('input');
  // 设置input类型为file，表示文件选择器
  input.type = 'file';
  input.accept = 'image/*';
  // 添加change事件监听器，当用户选择文件后触发
  input.addEventListener('change', function (event) {
    // 获取选择的文件
    const selectedFile = event.target.files[0];
    // 在这里你可以处理选择的文件，比如读取内容或上传到服务器等
    console.log('选择的文件:', selectedFile);
    //文件的Base64字符串
    // const base64 = getBase64Image(readImg(selectedFile));
    //Base64字符串转二进制
    // const file = dataURLtoBlob(base64);
    const file = fileToBlob(selectedFile);

    const msg = MpcUtil.sendPic(1, currentChat.id, file);
    msg.my = true;
    msg.src = window.URL.createObjectURL(selectedFile);
    msgHis.value.push(msg);
  });
  // 触发文件选择器
  input.click();
}
// 添加录音时长相关的响应式变量
const recordDuration = ref(0);
const recordTimer = ref(null);

// 修改 sendAudio 函数
function sendAudio() {
  if (mediaRecorder.state === "recording") {
    mediaRecorder.stop();
    sendAudioText.value = "点击开始录音";
    console.log("录音结束");
    // 停止计时器
    clearInterval(recordTimer.value);
    recordDuration.value = 0;
  } else {
    mediaRecorder.start();
    console.log("录音中...");
    // 开始计时
    recordDuration.value = 0;
    recordTimer.value = setInterval(() => {
      recordDuration.value++;
    }, 1000);
    sendAudioText.value = "点击停止录音";
  }
}


// 连接服务器
MpcUtil.connect('ws://192.168.1.100:5219',()=>{
  console.log("连接成功！")
}, (msg)=>{
  console.log(msg);
  switch (msg.type) {
    case 51: {
      msg.my = false;
      msgHis.value.push(msg);
      break;
    }
    case 54: {
      const buffer = MpcUtil.mergeAudioResponse(msg);
      if (buffer !== undefined) {
        const blob = new Blob([buffer], {type: "audio/mp3"});
        msg.my = false;
        msgHis.value.push({
          id: msg.id,
          type: 54,
          fromId: msg.fromId,
          toType: msg.toType,
          toId: msg.toId,
          src: window.URL.createObjectURL(blob)
        });
      }
      break;
    }
    case 55: {
      const buffer = MpcUtil.mergeAudioResponse(msg);
      if (buffer !== undefined) {
        const blob = new Blob([buffer], {type: "image/png;codecs=opus"});
        msg.my = false;
        msgHis.value.push({
          type: 55,
          fromId: msg.fromId,
          toType: msg.toType,
          toId: msg.toId,
          src: window.URL.createObjectURL(blob)
        });
      }
      break;
    }
    case 201: {
      msg.my = false;
      msgHis.value.push(msg);
      break;
    }
    default: {
      console.log("未知消息！")
    }
  }
});


// watch(currentChat, (newVal, oldVal) => {
//   needChat(newVal);
// });

// 查询好友信息
getRelation().then(resp=>{
  if(resp.code === 1) {
    friendList.value = resp.data;
  }
}).catch(err=>{
});

// 搜索过滤函数
function filterList(list) {
  if (!searchText.value) return list;
  return list.filter(item =>
    item.username.toLowerCase().includes(searchText.value.toLowerCase()) ||
    item.account.toLowerCase().includes(searchText.value.toLowerCase())
  );
}

// 点击外部关闭下拉菜单
function handleClickOutside(event) {
  const dropdown = document.querySelector('.dropdown-menu');
  const btn = document.querySelector('.multi-btn');
  if (dropdown && !dropdown.contains(event.target) && !btn.contains(event.target)) {
    showDropdown.value = false;
  }
}

// 添加和移除点击外部事件监听
onMounted(() => {
  document.addEventListener('click', handleClickOutside);
});

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside);
});

// 创建群组
function createGroup() {
  if (!groupName.value) {
    // 可以添加提示
    return;
  }
  // TODO: 调用创建群组的API
  showCreateGroup.value = false;
  groupName.value = '';
  selectedFriends.value = [];
}

/**
 * 显示聊天记录
 */
function showChatHistory() {
  isShowRight.value = false;
  showHistory.value = true;
  // 重置筛选条件
  historySearchText.value = "";
  messageTypeFilter.value = "all";
  timeRangeFilter.value = "all";
  activeMessageTab.value = "all";
}

/**
 * 过滤聊天记录
 */
function filterHistoryMessages() {
  return msgHis.value.filter(msg => {
    // 搜索文字过滤
    if (historySearchText.value && msg.type === 51) {
      if (!msg.content.toLowerCase().includes(historySearchText.value.toLowerCase())) {
        return false;
      }
    }
    
    // 消息类型过滤
    if (messageTypeFilter.value !== "all") {
      if (messageTypeFilter.value === "text" && msg.type !== 51) return false;
      if (messageTypeFilter.value === "audio" && msg.type !== 54) return false;
      if (messageTypeFilter.value === "image" && msg.type !== 55) return false;
    }
    
    // 时间范围过滤
    if (timeRangeFilter.value !== "all") {
      const msgDate = new Date(msg.time);
      const now = new Date();
      const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
      const weekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
      const monthAgo = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate());
      
      if (timeRangeFilter.value === "today" && msgDate < today) return false;
      if (timeRangeFilter.value === "week" && msgDate < weekAgo) return false;
      if (timeRangeFilter.value === "month" && msgDate < monthAgo) return false;
    }
    
    return true;
  });
}

/**
 * 获取指定类型的消息
 */
function getMessagesByType(type) {
  return filterHistoryMessages().filter(msg => {
    if (type === "all") return true;
    if (type === "text") return msg.type === 51 || msg.type === 1;
    if (type === "audio") return msg.type === 54 || msg.type === 4;
    if (type === "image") return msg.type === 55 || msg.type === 5;
    return false;
  });
}

</script>
<template>
  <!-- 消息对话框 -->
  <el-dialog
    v-model="isShow.messageDrawer"
    class="messageDialog"
    :draggable="true"
    align-center
    :show-close="false"
    :style="{'transform': isFullscreen?'scale(1.4) !important':'scale(1) !important'}">
    <template #header="{ close, titleId, titleClass }">
      <table style="width: 100%;"><tbody>
        <tr>
          <td width="130">聊天</td>
          <td style="color: rgb(100,100,100); text-align: center;">{{currentChat.username}}</td>
          <td width="40">
            <FullScreen
              class="clickBtn1"
              @click="isFullscreen = !isFullscreen"
              style="width: 20px; height: 20px; margin: 0 0 0 0; padding: 0; transform: scale(1.5); border-radius: 5px; float: right;"
            />
          </td>
          <td width="40" v-if="currentChat.id!==undefined">
            <More
              class="clickBtn1"
              @click="isShowRight=true"
              style="width: 20px; height: 20px; margin: 0; padding: 0; transform: scale(1.5); border-radius: 5px; float: right;"
            />
          </td>
        </tr>
      </tbody></table>
    </template>
    <div class="sidebar">
      <div class="box" v-if="boxIndex===1">
        <div class="search-box">
          <input
            type="text"
            v-model="searchText"
            placeholder="搜索聊天..."
            class="search-input"
          >
          <div class="dropdown">
            <button class="multi-btn" @click="showDropdown = !showDropdown">
              <Menu style="transform: scale(1.2)"></Menu>
            </button>
            <div class="dropdown-menu" v-show="showDropdown">
              <div class="dropdown-item" @click="showCreateGroup = true; showDropdown = false">
                <span>创建群组</span>
              </div>
            </div>
          </div>
        </div>
        <div v-show="filterList(chatList).length===0" style="width: 100%; text-align: center; margin-top: 200px; float: left;">
          {{ searchText ? '未找到匹配的聊天' : '当前无聊天' }}
        </div>
        <div class="friendBox" tabindex="0" v-for="chat in filterList(chatList)" :key="chat.id" @click="chatting(chat)"
             :style="{
                'background-color': chat.id===currentChat.id?'rgba(242,244,245,1)':'',
                'box-shadow': chat.id===currentChat.id?'0 0 4px 1px rgba(230,232,244,1) inset !important':''
             }">
          <img class="avatar" :src="chat.avatarUrl">
          <span class="name">{{chat.username}}</span>
        </div>
      </div>
      <div class="box" v-else>
        <div class="search-box">
          <input
            type="text"
            v-model="searchText"
            placeholder="搜索好友..."
            class="search-input"
          >
          <div class="dropdown">
            <button class="multi-btn" @click="showDropdown = !showDropdown">
              <Menu style="transform: scale(1.2)"></Menu>
            </button>
            <div class="dropdown-menu" v-show="showDropdown">
              <div class="dropdown-item" @click="showCreateGroup = true; showDropdown = false">
                <span>创建群组</span>
              </div>
            </div>
          </div>
        </div>
        <div class="friendBox" v-for="friend in filterList(friendList)" :key="friend.id" @click="needChat(friend)">
          <img class="avatar" :src="friend.avatarUrl">
          <span class="name">{{friend.username}}</span>
        </div>
      </div>
      <div class="bottomBox">
        <div class="bottomBoxBtn" :style="{'color': boxIndex===1?'white':'black', background: boxIndex===1?'#409EFF':''}" @click="boxIndex=1">聊天</div>
        <div class="bottomBoxBtn" :style="{'color': boxIndex===2?'white':'black', background: boxIndex===2?'#409EFF':''}" @click="boxIndex=2">通讯录</div>
      </div>
    </div>
    <div class="chatBox">
      <div v-if="currentChat.id===undefined && !showCreateGroup && !showHistory" style="width: 585px; height: 250px; padding-top: 250px; border-radius: 15px; text-align: center;">当前无聊天</div>
      <div v-else-if="showCreateGroup">
        <div class="create-group-container">
          <div class="create-group-header">
            <h2>创建群组</h2>
            <button class="close-btn" @click="showCreateGroup = false">
              <Back style="transform: scale(1.4)"></Back>
            </button>
          </div>
          <div class="create-group-form">
            <div class="form-item">
              <input type="text" v-model="groupName" placeholder="请输入群组名称" class="group-name-input">
            </div>
            <div class="form-item friend-list-container">
              <div class="friend-list">
                <div v-for="friend in friendList" :key="friend.id"
                     class="friend-item"
                     :class="{'selected': selectedFriends.includes(friend.id)}"
                     @click="selectedFriends.includes(friend.id) ?
                            selectedFriends = selectedFriends.filter(id => id !== friend.id) :
                            selectedFriends.push(friend.id)">
                  <img :src="friend.avatarUrl" class="friend-avatar">
                  <span class="friend-name">{{friend.username}}</span>
                </div>
              </div>
            </div>
            <div class="form-actions">
              <button class="cancel-btn" @click="showCreateGroup = false">取消</button>
              <button class="create-btn" @click="createGroup">创建</button>
            </div>
          </div>
        </div>
      </div>
      <div v-else-if="showHistory">
        <div class="chat-history-container">
          <div class="chat-history-header">
            <h2>聊天记录</h2>
            <button class="close-btn" @click="showHistory = false">
              <Back style="transform: scale(1.4)"></Back>
            </button>
          </div>
          <div class="chat-history-search">
            <div class="search-input-wrapper">
              <input
                type="text"
                v-model="historySearchText"
                placeholder="搜索消息内容..."
                class="history-search-input"
              >
            </div>
            <div class="filter-options">
              <select v-model="timeRangeFilter" class="filter-select">
                <option value="all">全部时间</option>
                <option value="today">今天</option>
                <option value="week">最近一周</option>
                <option value="month">最近一月</option>
              </select>
            </div>
          </div>
          <div class="message-tabs">
            <div 
              class="tab-item" 
              :class="{ active: activeMessageTab === 'all' }"
              @click="activeMessageTab = 'all'"
            >
              全部
            </div>
            <div 
              class="tab-item" 
              :class="{ active: activeMessageTab === 'text' }"
              @click="activeMessageTab = 'text'"
            >
              文字
            </div>
            <div 
              class="tab-item" 
              :class="{ active: activeMessageTab === 'audio' }"
              @click="activeMessageTab = 'audio'"
            >
              语音
            </div>
            <div 
              class="tab-item" 
              :class="{ active: activeMessageTab === 'image' }"
              @click="activeMessageTab = 'image'"
            >
              图片
            </div>
          </div>
          <div class="chat-history-content">
            <div v-if="getMessagesByType(activeMessageTab).length === 0" class="no-messages">
              没有找到匹配的消息
            </div>
            <div v-else class="history-list">
              <template v-if="activeMessageTab === 'all'">
                <div v-for="(msg, index) in getMessagesByType('all')" :key="index" class="history-item" :class="{'my-message': msg.my}">
                  <div class="message-time">{{ msg.time ? new Date(msg.time).toLocaleString() : '未知时间' }}</div>
                  <div class="message-content">
                    <template v-if="msg.type === 1 || msg.type === 51">
                      {{ msg.content }}
                    </template>
                    <template v-else-if="msg.type === 4 || msg.type === 54">
                      <div v-if="msg.src">
                        <audio controls :src="msg.src" class="audio-player"></audio>
                      </div>
                      <div v-else class="placeholder-message">语音消息</div>
                    </template>
                    <template v-else-if="msg.type === 5 || msg.type === 55">
                      <img v-if="msg.src" :src="msg.src" class="message-image">
                      <div v-else class="placeholder-message">图片消息</div>
                    </template>
                  </div>
                </div>
              </template>
              <template v-else-if="activeMessageTab === 'text'">
                <div v-for="(msg, index) in getMessagesByType('text')" :key="index" class="history-item text-message" :class="{'my-message': msg.my}">
                  <div class="message-time">{{ msg.time ? new Date(msg.time).toLocaleString() : '未知时间' }}</div>
                  <div class="message-content">{{ msg.content }}</div>
                </div>
              </template>
              <template v-else-if="activeMessageTab === 'audio'">
                <div class="audio-grid">
                  <div v-for="(msg, index) in getMessagesByType('audio')" :key="index" class="audio-item">
                    <div class="message-time">{{ msg.time ? new Date(msg.time).toLocaleString() : '未知时间' }}</div>
                    <div v-if="msg.src">
                      <audio controls :src="msg.src" class="audio-player"></audio>
                    </div>
                    <div v-else class="placeholder-message">语音消息</div>
                  </div>
                </div>
              </template>
              <template v-else-if="activeMessageTab === 'image'">
                <div class="image-grid">
                  <div v-for="(msg, index) in getMessagesByType('image')" :key="index" class="image-item">
                    <div class="message-time">{{ msg.time ? new Date(msg.time).toLocaleString() : '未知时间' }}</div>
                    <div v-if="msg.src">
                      <img :src="msg.src" class="message-image">
                    </div>
                    <div v-else class="placeholder-message">图片消息</div>
                  </div>
                </div>
              </template>
            </div>
          </div>
        </div>
      </div>
      <div v-else>
        <div class="chatContent">
          <MessageBox :myUser="myUser" :currentChat="currentChat" :msgHis="msgHis" :width="'590px'"></MessageBox>
        </div>
        <div class="chatSend">
          <button class="textAndAudioBtn" @click="isText=!isText">
            <CpsJp v-if="isText" style="transform: scale(1.7)"></CpsJp>
            <CpsYy v-else style="transform: scale(1.7)"></CpsYy>
          </button>
          <input v-if="isText" v-model="chatText" @keyup.enter="send">
          <button id="mediaBtn" v-else class="mediaBtn inputBtn" @click="sendAudio">
            {{sendAudioText}} <span v-if="recordDuration > 0">({{recordDuration}}s)</span>
          </button>
          <ClickBtn theme="green" @click="send">
            <Navigation style="transform: scale(1.7)"></Navigation>
          </ClickBtn>
          <UploadBtn @click="sendPic">
            <UploadPic style="transform: scale(1.7)"></UploadPic>
          </UploadBtn>
          <ClickBtn>
            <CpsTj style="transform: scale(1.7)"></CpsTj>
          </ClickBtn>
        </div>
      </div>
    </div>
    <!-- 添加遮罩层 -->
    <Transition name="fade">
      <div class="mask" v-show="isShowRight" @click="isShowRight=false"></div>
    </Transition>

    <div :style="{'right': isShowRight?'0':'-210px'}" class="friendInfoBox">
      <table style="width: calc(100%);" align="center">
        <tbody align="center">
        <tr>
          <td align="left" width="20">
            <ClickBtn @click="isShowRight=false" style="margin: 0">
              <Back style="transform: scale(1.4)"></Back>
            </ClickBtn>
          </td>
          <td align="right" style="font-size: 16px; color: black; padding-right: 10px">详细信息</td>
        </tr>
        <tr>
          <td colspan="2">
            <img class="avatar" :src="currentChat.avatarUrl" style="margin-top: 10px">
          </td>
        </tr>
        <tr>
          <td colspan="2" style="color: black; font-size: 20px">{{currentChat.username}}</td>
        </tr>
        <tr>
          <td colspan="2">{{currentChat.account}}</td>
        </tr>
        <tr>
          <td colspan="2">内容 0 | 粉丝 {{currentChat.fansNum}} | 关注 0</td>
        </tr>
        <tr>
          <td>{{currentChat.email}}</td>
        </tr>
        <tr>
          <td colspan="2" style="padding: 20px 0;">
            <button class="history-btn" @click="showChatHistory">
              <span>查看聊天记录</span>
            </button>
          </td>
        </tr>
        </tbody>
      </table>
    </div>
  </el-dialog>
</template>
<style scoped>
/* 侧边栏样式 */
.sidebar {
  height: 500px;
  width: 144px;
  float: left;
  background-color: rgba(251,253,255,0.1);
  overflow: hidden;
  position: relative;
}

.box {
  width: 100%;
  height: calc(100% - 40px);
  background-color: rgba(250, 249, 249, 0);
  overflow-y: auto;
  margin-bottom: 10px;
  display: flex;
  flex-direction: column;
}

/* 底部导航按钮 */
.bottomBox {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 40px;
  background-color: rgba(251,253,255,0);
}

.bottomBoxBtn {
  margin: 0 2px;
  height: 40px;
  width: 68px;
  float: left;
  text-align: center;
  line-height: 40px;
  user-select: none;
  transition: all 0.2s ease-in-out;
  background-color: rgba(0,0,0,0);
  border-radius: 10px;
}

.bottomBoxBtn:hover {
  background-color: rgba(220,220,225,1);
}

/* 好友列表项 */
.friendBox {
  height: 50px !important;
  width: 130px;
  margin: 7px 7px 0 7px;
  border-radius: 15px;
  box-shadow: 0 0 1px rgba(200, 200, 200, 1);
  transition: all 0.2s ease-in-out;
  position: relative;
  cursor: pointer;
}

.friendBox:hover {
  box-shadow: 0 0 3px 2px rgba(220, 222, 228, 1);
  transform: scale(1.01);
}

.friendBox .avatar {
  width: 36px;
  margin: 7px 7px;
  border-radius: 50px;
  user-select: none;
  float: left;
}

.friendBox .name {
  width: 60px;
  float: left;
  line-height: 50px;
  user-select: none;
}



.chatBox {
  height: 500px;
  width: 590px;
  float: left;
  border-radius: 10px;
  box-shadow: 0 0 1px 0 rgba(100,100,100,1);
  overflow: hidden;
  margin-left: 2px;
  background-color: rgba(251,253,255,0.1);
}

.chatTitle {
  width: 100%;
  height: 50px;
  box-shadow: 0 0 1px 0 rgba(100,100,100,1);
}
.chatTitle .avatar {
  width: 40px;
  margin: 5px 10px 5px 5px !important;
  border-radius: 50px;
  user-select: none;
  float: left;
}
.chatTitle .name {
  width: 100px;
  float: left;
  line-height: 50px;
  user-select: none;
}

.chatContent {
  width: 100%;
  height: calc(500px - 51px - 7px);
  background-color: rgba(245,247,250,1);
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  padding-bottom: 7px;
}
/*定义了滚动条整体的样式*/
.chatContent::-webkit-scrollbar {
  position: absolute;
  right: 0;
  top: 0;
  width: 3px;
}
/*滑块部分*/
.chatContent::-webkit-scrollbar-thumb {
  width: 100%;
  background-color: rgba(120,120,120,0.5);
}
/*轨道部分*/
.chatContent::-webkit-scrollbar-track {

}


.chatSend {
  width: 100%;
  height: 50px;
  border-top: 1px solid rgba(200,200,200,1);
}

/* 文字或语音按钮 */
.textAndAudioBtn {
  background-color: rgba(0,0,0,0);
  height: 36px;
  margin: 7px;
  border: 0;
  width: 36px;
  border-radius: 20px;
  line-height: 36px;
  padding: 3px 0 0 0;
  transition: all 0.1s ease-in-out;
  float: left;
}
.textAndAudioBtn:hover {
}
.textAndAudioBtn:active {
  transform: scale(0.9);
}

input {
  border: 0;
  box-shadow: 0 0 1px 0 rgb(1,1,1,1);
  border-radius: 10px;
  height: 32px;
  outline: none; /* 去除选中状态边框 */
  float: left;
  margin: 8px 0;
  padding: 1px 5px;
  width: 391px;
  transition: all 0.1s ease-in-out;
}
input:focus {
  background-color: rgba(240,240,242,1);
}

.inputBtn {
  user-select: none;
  float: left;
  height: 36px;
  width: 401px;
  margin: 7px 0;
  border-radius: 10px;
  line-height: 36px;
  transition: all 0.1s ease-in-out;
  border: 0;
  padding: 0 20px;
  background-color: rgba(240,242,245,1);
}
.inputBtn:hover {
  background-color: rgba(230,232,235,1);
}
.inputBtn:active {
  background-color: #409EFF;
  transform: scale(0.95);
}

.clickBtn1 {
  background-color: rgba(0,0,0,0);
  height: 36px;
  margin: 7px 0 7px 7px;
  border: 0;
  width: 36px;
  border-radius: 10px;
  line-height: 36px;
  padding: 3px 0 0 0;
  transition: all 0.1s ease-in-out;
  float: left;
}
.clickBtn1:hover {
  background-color: rgba(220,220,225,1);
}
.clickBtn1:active {
  background-color: #409EFF;
  transform: scale(1.3) !important;
}


.friendInfoBox {
  position: absolute;
  top: 0;
  width: 200px;
  height: 547px;
  background-color: rgba(241, 243, 245, 0.9); /* 提高不透明度 */
  backdrop-filter: blur(20px); /* 减少模糊程度 */
  overflow-y: auto;
  transition: 0.2s ease-in-out;
  z-index: 2; /* 确保在遮罩层上方 */
  border-radius: 10px;
}

.friendInfoBox .avatar {
  border-radius: 100px;
  width: 60px;
  height: 60px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.15); /* 降低透明度 */
  z-index: 1; /* 移除了模糊效果 */
}

.search-box {
  margin: 0 8px;
  border-bottom: 1px solid #e4e7ed;
  flex-shrink: 0;
  width: 127px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.search-input {
  width: 90px;
  height: 30px;
  border: 1px solid #dcdfe6;
  border-radius: 10px;
  background-color: #f5f7fa;
  color: #606266;
  font-size: 14px;
  transition: all 0.3s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.dropdown {
  position: relative;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 4px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  min-width: 100px;
  z-index: 1000;
  overflow: hidden;
}

.dropdown-item {
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: all 0.3s;
  color: #606266;
}

.dropdown-item:hover {
  background-color: #f5f7fa;
}

.dropdown-item span {
  font-size: 14px;
}

.multi-btn {
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 10px;
  background-color: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.multi-btn:hover {
  background-color: #e4e7ed;
}

.multi-btn:active {
  transform: scale(0.95);
}

.create-group-container {
  height: 500px;
  width: 590px;
  background-color: rgba(245,247,250,1);
  display: flex;
  flex-direction: column;
  border-radius: 10px;
  box-shadow: 0 0 1px 0 rgba(100,100,100,1);
  overflow: hidden;
}

.create-group-header {
  padding: 10px 20px;
  border-bottom: 1px solid rgba(200,200,200,1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  height: 50px;
}

.create-group-header h2 {
  margin: 0;
  color: #333;
  font-size: 16px;
  font-weight: 500;
}

.close-btn {
  background: none;
  border: none;
  padding: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.2s ease-in-out;
}

.close-btn:hover {
  background-color: rgba(240,242,245,1);
}

.close-btn:active {
  transform: scale(0.95);
}

.create-group-form {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  overflow: hidden;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.group-name-input {
  border: 0;
  box-shadow: 0 0 1px 0 rgb(1,1,1,1);
  border-radius: 10px;
  height: 36px;
  outline: none;
  padding: 0 15px;
  width: calc(100% - 30px);
  transition: all 0.2s ease-in-out;
  background-color: #fff;
  font-size: 14px;
}

.group-name-input:focus {
  background-color: rgba(240,240,242,1);
  box-shadow: 0 0 3px 2px rgba(220, 222, 228, 1);
}

.friend-list-container {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border-radius: 10px;
}

.friend-list {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
  padding: 12px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 1px 0 rgb(1,1,1,1);
  overflow-y: auto;
}

.friend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
  background-color: #fff;
  border: 1px solid transparent;
  height: 44px;
}

.friend-item:hover {
  box-shadow: 0 0 3px 2px rgba(220, 222, 228, 1);
  transform: scale(1.01);
  border-color: rgba(220, 222, 228, 1);
}

.friend-item.selected {
  background-color: #ecf5ff;
  box-shadow: 0 0 4px 1px rgba(64, 158, 255, 0.2) inset !important;
  border-color: #409EFF;
  position: relative;
}

.friend-item.selected::after {
  content: '';
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  background-color: #409EFF;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 10px;
  box-shadow: 0 0 4px rgba(64, 158, 255, 0.4);
}

.friend-item.selected::before {
  content: '✓';
  position: absolute;
  right: 11px;
  top: 50%;
  transform: translateY(-50%);
  color: white;
  font-size: 10px;
  z-index: 1;
}

.friend-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50px;
  object-fit: cover;
  border: 2px solid transparent;
  transition: all 0.2s ease-in-out;
}

.friend-item.selected .friend-avatar {
  border-color: #409EFF;
}

.friend-name {
  font-size: 13px;
  color: #333;
  font-weight: 500;
  transition: all 0.2s ease-in-out;
}

.friend-item.selected .friend-name {
  color: #409EFF;
  font-weight: 600;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: auto;
  padding-top: 20px;
  border-top: 1px solid rgba(200,200,200,1);
}

.cancel-btn,
.create-btn {
  padding: 8px 24px;
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
  border: 0;
  font-weight: 500;
}

.cancel-btn {
  background-color: rgba(240,242,245,1);
  color: #606266;
}

.create-btn {
  background-color: #409EFF;
  color: #fff;
}

.cancel-btn:hover {
  background-color: rgba(230,232,235,1);
}

.create-btn:hover {
  background-color: #66b1ff;
}

.cancel-btn:active,
.create-btn:active {
  transform: scale(0.95);
}

/* 定义滚动条样式 */
.friend-list::-webkit-scrollbar {
  position: absolute;
  right: 0;
  top: 0;
  width: 3px;
}

.friend-list::-webkit-scrollbar-thumb {
  width: 100%;
  background-color: rgba(120,120,120,0.5);
}

.history-btn {
  width: 160px;
  height: 36px;
  background-color: #409EFF;
  color: white;
  border: none;
  border-radius: 18px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
}

.history-btn:hover {
  background-color: #66b1ff;
  transform: scale(1.02);
}

.history-btn:active {
  transform: scale(0.98);
}

.chat-history-container {
  height: 500px;
  width: 590px;
  background-color: rgba(245,247,250,1);
  display: flex;
  flex-direction: column;
  border-radius: 10px;
  box-shadow: 0 0 1px 0 rgba(100,100,100,1);
  overflow: hidden;
}

.chat-history-header {
  padding: 10px 20px;
  border-bottom: 1px solid rgba(200,200,200,1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  height: 50px;
}

.chat-history-header h2 {
  margin: 0;
  color: #333;
  font-size: 16px;
  font-weight: 500;
}

.chat-history-search {
  padding: 12px 20px;
  border-bottom: 1px solid rgba(200,200,200,1);
  display: flex;
  align-items: center;
  gap: 12px;
  background-color: #fff;
  flex-wrap: wrap;
}

.search-input-wrapper {
  flex: 1;
  min-width: 200px;
}

.history-search-input {
  width: 100%;
  height: 36px;
  border: 1px solid #dcdfe6;
  border-radius: 18px;
  background-color: #f5f7fa;
  color: #606266;
  font-size: 14px;
  padding: 0 15px;
  transition: all 0.3s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.history-search-input:focus {
  background-color: #fff;
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.filter-options {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  margin-left: 40px;
}

.filter-select {
  width: 140px;
  height: 36px;
  border: 1px solid #dcdfe6;
  border-radius: 18px;
  background-color: #f5f7fa;
  color: #606266;
  font-size: 14px;
  padding: 0 15px;
  transition: all 0.3s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23606266' d='M6 8.825L1.175 4 2.05 3.125 6 7.075 9.95 3.125 10.825 4z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 15px center;
  padding-right: 35px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.filter-select:hover {
  border-color: #c0c4cc;
  background-color: #fff;
}

.filter-select:focus {
  background-color: #fff;
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  outline: none;
}

/* 自定义下拉选项样式 */
.filter-select option {
  padding: 8px 12px;
  font-size: 14px;
  color: #606266;
  background-color: #fff;
  border: none;
}

.filter-select option:hover {
  background-color: #f5f7fa;
}

.filter-select option:checked {
  background-color: #ecf5ff;
  color: #409EFF;
  font-weight: 500;
}

/* 添加下拉菜单动画 */
@keyframes dropdownFade {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.filter-select:focus option {
  animation: dropdownFade 0.2s ease-out;
}

.message-tabs {
  display: flex;
  padding: 12px 20px;
  border-bottom: 1px solid rgba(200,200,200,1);
  background-color: #fff;
  gap: 8px;
}

.tab-item {
  padding: 8px 16px;
  border-radius: 18px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 14px;
  color: #606266;
  user-select: none;
}

.tab-item:hover {
  background-color: #f5f7fa;
}

.tab-item.active {
  background-color: #409EFF;
  color: #fff;
  box-shadow: 0 2px 4px rgba(64, 158, 255, 0.2);
}

.chat-history-content {
  flex: 1;
  overflow-y: auto;
  background-color: #f5f7fa;
  padding: 16px;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
}

.history-item {
  width: auto;
  max-width: 70%;
  padding: 12px 16px;
  border-radius: 12px;
  background-color: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
  position: relative;
  margin-bottom: 10px;
}

.history-item.my-message {
  align-self: flex-end;
  background-color: #ecf5ff;
  margin-left: auto;
}

.message-time {
  font-size: 12px;
  color: #909399;
  margin-bottom: 8px;
  white-space: nowrap;
}

.message-content {
  word-break: break-word;
  line-height: 1.5;
  color: #303133;
}

.placeholder-message {
  padding: 8px 12px;
  background-color: #f5f7fa;
  border-radius: 8px;
  color: #909399;
  font-size: 14px;
  display: inline-block;
  margin: 5px 0;
}

.audio-player {
  width: 100%;
  height: 36px;
  border-radius: 18px;
  margin: 5px 0;
}

.audio-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  width: 100%;
}

.audio-item {
  background-color: #fff;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
  transition: all 0.3s;
  width: 100%;
}

.audio-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.audio-item .message-time {
  font-size: 12px;
  color: #909399;
  margin-bottom: 8px;
}

.audio-item audio {
  width: 100%;
  height: 36px;
  border-radius: 18px;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
  width: 100%;
}

.image-item {
  background-color: #fff;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
  transition: all 0.3s;
  width: 100%;
}

.image-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.image-item .message-time {
  font-size: 12px;
  color: #909399;
  margin-bottom: 8px;
}

.image-item .message-image {
  width: 100%;
  height: 150px;
  object-fit: cover;
  border-radius: 8px;
  transition: all 0.3s;
}

.image-item:hover .message-image {
  transform: scale(1.02);
}

.text-message {
  max-width: 100% !important;
}

.text-message .message-content {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.6;
  color: #303133;
}

.no-messages {
  padding: 40px 20px;
  text-align: center;
  color: #909399;
  font-size: 14px;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
  width: calc(100% - 40px);
  margin: 0 auto;
}

/* 定义滚动条样式 */
.chat-history-content::-webkit-scrollbar {
  width: 3px;
}

.chat-history-content::-webkit-scrollbar-thumb {
  background-color: rgba(120,120,120,0.5);
  border-radius: 3px;
}

.chat-history-content::-webkit-scrollbar-track {
  background-color: transparent;
}
</style>
